package org.yefei.homeworkday24;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Seven {

	public static void main(String[] args) throws IOException {
		// File f = new File("e:/java61/a/b/c/f");
		// if (!f.exists()) {
		// f.mkdirs();
		// }
		// File f2 = new File("E:/java61/or2.jpg");
		// boolean r = f2.renameTo(new File("D:/home/" + f2.getName()));
		// System.out.println(r ? "移动成功" : "剪切失败");
		Scanner in = new Scanner(System.in);
		System.out.println("input a picture:");
		String path = in.nextLine();
		File f = new File(path);
		// 后缀：
		// String[]ar=f.getName().split("\\.");
		// ar[ar.length-1]
		FileInputStream fin = new FileInputStream(f);
		int[] b8 = new int[8];
		for (int i = 0; i < b8.length; i++) {
			// 读入8个字节，放入int数组
			b8[i] = fin.read();
		}
		// byte[]b8=new byte[8];
		// fin.read(b8);

		fin.close();
		String houzhui = f.getName().substring(1 + f.getName().lastIndexOf('.'));
		switch (houzhui) {
		case "bmp":
			if (b8[0] == 0X42 && b8[1] == 0X4D) {
				System.out.println("合法的bmp文件");
			} else {
				System.out.println("非法的bmp文件");
			}
			break;
		case "jpg":
			if (b8[0] == 0XFF && b8[1] == 0XD8) {
				System.out.println("合法的jpg文件");
			} else {
				System.out.println("非法的jpg文件");
			}
			break;
		case "png":
			int[] x = { 0X89, 0X50, 0X4E, 0X47, 0X0D, 0X0A, 0X1A, 0X0A };
			boolean e = Arrays.equals(b8, x);
			if (e) {
				System.out.println("合法的png");
			} else {
				System.out.println("非法的png");
			}
			break;
		}

	}

}
